
import { reqAddressInfo, reqShopInfo, reqSubmitOrder } from '@/api'

const state = {
    // 用户地址信息
    address: [],
    // 商品清单数据
    tradeInfo: {},
    // 订单号
    payId: '',

};
const mutations = {
    GETADDRESS(state, address) {
        state.address = address
    },

    GETSHOPINFO(state, tradeInfo) {
        state.tradeInfo = tradeInfo
    },
    SUBMITINFO(state, payId) {
        state.payId = payId
    },


};
const actions = {
    async getAddress({ commit, dispatch, state }) {
        let result = await reqAddressInfo()
        if (result.code == 200) {
            commit('GETADDRESS', result.data)
            return 'ok'
        } else {
            return Promise.reject(new Error(result.message))
        }
    },

    //    获取商品清单
    async getShopInfo({ commit, dispatch, state }) {
        let result = await reqShopInfo()
        // console.log(result.data)
        if (result.code == 200) {
            commit('GETSHOPINFO', result.data)
            return 'ok'
        } else {
            return Promise.reject(new Error(result.message))
        }
    },

    // 提交订单
    async submitInfo({ commit, dispatch, state }, { tradeNo, data }) {
        let result = await reqSubmitOrder(tradeNo, data)
        // console.log(result.data)
        if (result.code == 200) {
            commit('SUBMITINFO', result.data)
            return 'ok'
        } else {
            return Promise.reject(new Error(result.message))
        }

    }

};
const getters = {};

export default {
    state,
    mutations,
    actions,
    getters
}